<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<jsp:include page="./header.jsp" />

<%@page import="java.util.*"%>
<%@page import="util.*"%>
<%@page import="domain.*"%>  
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<%
	//List<Schedule> schedules = (List<Schedule>) request.getAttribute("schedules");
	//String message = (String) request.getAttribute("message");
	List<Person> drivers = (List<Person>) request.getAttribute("drivers");
	Person currentUser = (Person) request.getAttribute("currentUser");
%>
<title>Get Statistics</title>

<script type="text/javascript">
</script>

</head>
<body>
	<form id="homeSubmit">
		<input type="hidden" name="todo" value="home"/>
	</form>
	<div data-role="page" data-theme="a" id="mainPage">

	<div data-role="header" data-position="inline">
		<h1>Statistics</h1>
		<a href="javascript:homeSubmit('Login')" data-icon="home" class="ui-btn-right" data-iconpos="notext"></a>
	</div><!-- /header -->

	<div data-role="content" >
		<ul data-role="listview" data-inset="true" data-theme="a">
			<%
				for(Person p: drivers){
					Driver d = (Driver) p.getRoles().get(0);
			%>
					<li>
						<a href="#schedule<%= p.getId()%>" data-transition="slide" id="driverLink<%= p.getId()%>">
							<%=p.getFullName()%>
						</a>
					</li>
				<%} %>
			</ul>
	</div><!-- /content -->

	<div data-role="footer" class="ui-bar">
			<h4><%=currentUser.getFullName() %></h4>
	</div><!-- /footer -->
	</div><!-- /page -->
	
	<!-- list of schedules -->
	<%
		for(Person p: drivers){
			Driver d = (Driver) p.getRoles().get(0);
	%>
		<div data-role="page" id="schedule<%= p.getId()%>" data-theme="a">
			<div data-role="header" data-position="inline">
				<h1><%=p.getFullName()%></h1>
				<a href="javascript:homeSubmit('Login')" data-icon="home" class="ui-btn-right" data-iconpos="notext"></a>
			</div><!-- /header -->
			
			<div data-role="content">
				<ul data-role="listview" data-filter="true" data-theme="c">
					<%
					if(d.getSchedules()!=null){
						for(Schedule s: d.getSchedules()){ 
					%>
					<li>
						<a href="#mission<%=s.getId() %>" data-transition="slide" id="scheduleLink<%=s.getId() %>">
							<%=s.getRoute()%> - <%= DateUtil.getDDMMYYYYFromDate(s.getDate(), 0) %>
						</a>	
					</li>
						<%} %>
					<%} %>
				</ul>		
			</div><!-- /content -->
			<div data-role="footer" class="ui-bar">
				<h4><%=currentUser.getFullName() %></h4>
			</div><!-- /footer -->
		</div><!-- /page -->
	<%} %>
	
	<!-- list of missions -->
	<%
		for(Person p: drivers){
			Driver d = (Driver) p.getRoles().get(0);
			for(Schedule s: d.getSchedules()){
	%>
		<div data-role="page" id="mission<%= s.getId()%>" data-theme="a">
			<div data-role="header" data-position="inline">
				<h1><%=s.getRoute()%> - <%= DateUtil.getDDMMYYYYFromDate(s.getDate(), 0) %></h1>
				<a href="javascript:homeSubmit('Login')" data-icon="home" class="ui-btn-right" data-iconpos="notext"></a>
			</div><!-- /header -->
			
			<div data-role="content">
				<ul data-role="listview" data-theme="c" data-count-theme="c">
					<%
						for(Mission m: s.getMissions()){ 
					%>
					<li>
						<a href="#missionDetail<%=m.getId() %>" data-transition="slidedown" data-rel="dialog" id="missionLink<%=m.getId() %>">
							<%=m.getPriority() %>. <%=m.getToPOS().getName() %>
							<%if(m.getCompleteTime()!=null && m.getEndTime()!=null && m.getDelayDuration()!=0){ %>
								<span class="ui-li-count" >late</span>
							<%} %>
						</a>	
					</li>
					<%} %>
				</ul>		
			</div><!-- /content -->
			<div data-role="footer" class="ui-bar">
				<h4><%=currentUser.getFullName() %></h4>
			</div><!-- /footer -->
		</div><!-- /page -->
		<%} %>
	<%} %>
	
	<!-- mission's details -->
	<%
		for(Person p: drivers){
			Driver d = (Driver) p.getRoles().get(0);
			for(Schedule s: d.getSchedules()){
				for(Mission m: s.getMissions()){
					List<Envelope> envelopes = m.getEnvelopes();
	%>
		<div data-role="page" id="missionDetail<%= m.getId()%>" >
			<div data-role="header" data-position="inline">
				<h1><%=m.getToPOS().getName() %></h1>
			</div><!-- /header -->
			
			<div data-role="content">
				<ul data-role="listview" data-theme="c">
					<li>From: <%=m.getFromPOS().getName() %></li>
					<li>To: <%=m.getToPOS().getName() %></li>
					<li>Estimated: <%=m.getEndTime()!=null?  DateUtil.getDDMMYYYYHHMMFromTimestamp(m.getEndTime()):""%></li>
					<li>Actual: <%= m.getStatus().equals(MissionStatus.complete)? DateUtil.getDDMMYYYYHHMMFromTimestamp(m.getCompleteTime()):m.getStatus()%></li>
					<%if(m.getCompleteTime()!=null && m.getEndTime()!=null && m.getDelayDuration()!=0){ %>
						<li data-theme="c"><h1>Delay: <%=m.getDelayDuration()%> mins</li>
					<%} %>
					<%for(int k=0; k<envelopes.size(); k++){ 
						Envelope envelope = envelopes.get(k);
					%>
						<li><%=envelope.getEnvelopId() %> 
							(<%=envelope.getEnvelopeType()!=EnvelopeType.transfer? envelope.getEnvelopeType():(envelope.getTransferFrom().getPosId()==m.getToPOS().getPosId()? envelope.getEnvelopeType()+": pick":envelope.getEnvelopeType()+": drop")%>)
						</li>
					<%} %>
				</ul>			
			</div><!-- /content -->
			<div data-role="footer" class="ui-bar">
			</div><!-- /footer -->
		</div><!-- /page -->
			<%} %>
		<%} %>
	<%} %>
</body>
</html>